//
//  Controller14.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/7.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

// EmojiSpin
class Controller14: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

    let images = ["🌞","🌙","🌟","⚡️","⛈","❄️"]
    let pickerView = UIPickerView();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configUI();
        view.backgroundColor = .white;
    }

    func configUI(){
        pickerView.frame = CGRect(x: 0, y: 0, width: SCREEN_W, height: 250);
        pickerView.center = view.center;
        pickerView.delegate = self;
        pickerView.dataSource = self;
        pickerView.showsSelectionIndicator = true;
        view.addSubview(pickerView);
        
        let button = UIButton(frame: CGRect(x: 40, y: SCREEN_H - 80, width: SCREEN_W - 80, height: 40));
        button.setTitle("go", for: .normal);
        button.layer.cornerRadius = 3;
        button.layer.borderColor = UIColor.black.cgColor;
        button.layer.borderWidth = 1.0;
        button.setTitleColor(.black, for: .normal);
        button.addTarget(self, action: #selector(touchAction), for: .touchUpInside);
        view.addSubview(button);
    }
    
    func checkIsWin(){
        let s1 = images[pickerView.selectedRow(inComponent: 0) % images.count];
        let s2 = images[pickerView.selectedRow(inComponent: 1) % images.count];
        let s3 = images[pickerView.selectedRow(inComponent: 2) % images.count];
        if s1 == s2 && s2 == s3 {
            view.backgroundColor = UIColor.randomColor();
        }
    }
    
    func touchAction(){
        let all = images.count * 50 - 5 ; //防止尾部看到空白，留出5个空白。
        let component1 = arc4random_uniform(UInt32(all)) + 2; //防止头部看到空白，留出2个空白
        let component2 = arc4random_uniform(UInt32(all)) + 2;
        let component3 = arc4random_uniform(UInt32(all)) + 2;
        self.pickerView.selectRow(Int(component1), inComponent: 0, animated: true);
        self.pickerView.selectRow(Int(component2), inComponent: 1, animated: true);
        self.pickerView.selectRow(Int(component3), inComponent: 2, animated: true);
        checkIsWin();
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3;
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return images.count * 50;
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var label : UILabel
        if view == nil {
            label =  UILabel();
            label.textAlignment = .center;
        }
        else {
            label = view as! UILabel
        }
        let string = images[row % (images.count)];
        let attriString = NSAttributedString(string: string, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 80)]);
        label.attributedText = attriString
        return label;
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 100;
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    

}
